<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-form :inline="true" :model="TeacherQuery" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="TeacherQuery.name" placeholder="用户姓名" @keyup.enter.native="getList(1)"></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="TeacherQuery.level" placeholder="选择用户头衔">
          <el-option label="高级讲师" value="1"></el-option>
          <el-option label="首席讲师" value="2"></el-option>
          <el-option label="查询全部" value=""></el-option>
        </el-select>
      </el-form-item>


      <el-date-picker
        style="margin-bottom: 20px"
        v-model="TeacherQuery.begin"
        type="datetime"
        value-format="yyyy-MM-dd HH:mm:ss"
        placeholder="开始时间"
      >
      </el-date-picker>

      <el-date-picker
        style="margin-bottom: 20px"
        v-model="TeacherQuery.end"
        type="datetime"
        value-format="yyyy-MM-dd HH:mm:ss"
        placeholder="结束时间"
      >
      </el-date-picker>


      <el-form-item style="margin-left: 10px">
        <el-button type="primary" @click="getList(1)">查询</el-button>
        <el-button plain @click="resetData()">清除</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="数据加载中"
      border
      fit
      highlight-current-row>

      <el-table-column
        align="center"
        width="70"
        label="序号">

        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1}}
        </template>

      </el-table-column>

      <el-table-column align="center" prop="name" label="讲师姓名" width="86"/>

      <el-table-column label="头衔" align="center" width="120">
        <template slot-scope="scope">
          {{scope.row.level===1?'高级讲师':'首席讲师'}}
        </template>
      </el-table-column>

      <el-table-column prop="intro" label="讲师简介" align="center"/>

      <el-table-column prop="gmtCreate" label="添加时间" align="center"/>

      <el-table-column prop="sort" label="排序" width="60" align="center"/>

      <el-table-column align="center" label="操作" width="200">

        <template slot-scope="scope">
          <router-link :to="'/edu/table/'+scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-edit" style="margin-right: 4px">修改</el-button>
          </router-link>

            <el-popconfirm title="确定要删除吗？" @onConfirm="deleteTeacher(scope.row.id)">
              <el-button
                slot="reference" type="danger" size="mini"
                icon="el-icon-delete" style="margin-right: 4px"
              > 删除
              </el-button>
            </el-popconfirm>

        </template>
      </el-table-column>
    </el-table>

    <!-- 分页条 -->
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      :page-size="limit"
      :current-page="page"
      style="padding: 30px 0;text-align: center"
      @current-change="getList"
    >
    </el-pagination>

  </div>
</template>

<script>
  import teacher from '@/api/edu/teacher'

  export default {
    name: 'list',
    data() {
      return {
        page: 1, // 当前页面
        limit: 5, // 每页条数
        total: 0, // 总记录数
        list: null, // 查询接口后，返回的数据
        listLoading: true,// 表格加载页
        TeacherQuery: {}// 条件封装对象
      }
    },
    created() { // 页面渲染之前钩子函数
      this.getList()
    },
    methods: {

      // 加载数据
      getList(page = 1) { // element-ui 封装好了，点击按钮，传进来要去的页数
        teacher.getTeacherListPage(this.page = page, this.limit, this.TeacherQuery)
          // 请求成功
          .then(res => {
            this.list = res.data.rows
            this.total = res.data.total
            this.listLoading = false
          })
          // 请求失败
          .catch(error => {
            this.listLoading = false
            // this.$message.error('加载失败，请稍后重试');
            console.log(error)
          })
      },

      // 清除方法
      resetData() {
        this.TeacherQuery = {}
        this.getList()
      },

      // 删除方法
      deleteTeacher(id) {

        teacher.deleteById(id)
          .then(res => {
            if (res.success) {
              this.getList(this.page)
              this.$message.success("删除成功")
            }
          }).catch(err => {
          this.$message.error(err.message)
        })

      }
    }
  }
</script>

<style scoped>

</style>
